using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Library.WebControls.SimpleSplitContainer {
	[ToolboxBitmap(typeof(SpSplitContainer), "Library.WebControls.SimpleSplitContainer.SpSplitContainer.bmp")]
	[Designer(typeof(SpSplitContainerDesign))]
	[SupportsEventValidation]
	[DefaultProperty("Panels")]
	[ToolboxData("<{0}:SPSplitContainer runat=server></{0}:SPSplitContainer>")]
	//[ToolboxItem(true)]
	[DesignTimeVisible(true)]
	public class SpSplitContainer : BaseControl {
		//private List<Spliter> _renderSpliterList;
		private Table _mainTable;
		private SpSpliterPanelCollection _panels;

		//public SpSplitContainer() {
		//  CssFileName = BaseCssDir + "spspliter-ui.css";
		//}
		private string _cssFileName = BaseCssDir + "spspliter-ui.css";
		[DefaultValue("~/style/simple/spspliter-ui.css")]
		public override string CssFileName {
			get { return _cssFileName; }
			set {
				_cssFileName = value;
			}
		}

		public override string ID {
			get {
				EnsureChildControls();
				return _mainTable.ID;
			}
			set {
				EnsureChildControls();
				_mainTable.ID = value;
			}
		}

		public override string ClientID {
			get {
				EnsureChildControls();
				return _mainTable.ClientID;
			}
		}

		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

		[DefaultValue("")]
		public SpSpliterPanelCollection Panels {
			get {
				EnsureChildControls();
				return _panels;
			}
		}
		[DefaultValue(0)]
		public RepeatDirection Direction { get; set; }

		public Unit Width {
			get {
				EnsureChildControls();
				return _mainTable.Width;
			}

			set {
				EnsureChildControls();
				_mainTable.Width = value;
			}
		}

		public Unit Height {
			get {
				EnsureChildControls();
				return _mainTable.Height;
			}

			set {
				EnsureChildControls();
				_mainTable.Height = value;
			}
		}

		protected override void CreateChildControls() {
			_mainTable = new SpSpliterTable();
			Controls.Add(_mainTable);
			_panels = new SpSpliterPanelCollection(_mainTable);
		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);
			IncludeScriptOnLoad(typeof(SpSplitContainer), "Ky",
													"Library.WebControls.SimpleSplitContainer.include.SpSimpleSplitContainer.js", UtilLib);
		}

		protected virtual void RenderSpliter() {
			_mainTable.Controls.Clear();
			switch (Direction) {
				case RepeatDirection.Horizontal:
					HorizoneRender();
					break;
				case RepeatDirection.Vertical:
					VerticalRender();
					break;
			}
		}

		protected void HorizoneRender() {
			//var list = new List<Spliter>();
			//_renderSpliterList = list;
			TableRow row = new SpSpliterRow();
			TableCell previewCell = null;
			_mainTable.Rows.Add(row);
			if (Panels.Count > 0) {
				previewCell = Panels[0].CreateCell(String.Empty);//.CreateCell(ClientID + "pn0");
				row.Cells.Add(previewCell);
			}
			for (int i = 1; i < Panels.Count; i++) {
				var panel = Panels[i];
				TableCell spliterCell = new SpSpliterTableCell();

				spliterCell.CssClass = "spSpliterH";
				//spliterCell.Width = new Unit(5);
				row.Cells.Add(spliterCell);
				if (SpliterControlAdded != null) {
					SpliterControlAdded(this, new SpliterControlAddedEventArgs(spliterCell));
				}

				TableCell cell = panel.CreateCell(String.Empty);
				row.Cells.Add(cell);
				var minLeft = 0;
				var minRight = 0;
				//var s = new Spliter();
				if (previewCell != null) {
					//s.LeftId = previewCell.ClientID;
					/*s.MinLeft*/
					minLeft = ((SpSpliterPanel)previewCell.Controls[0]).MinValue;
				}
				//s.MidleId = spliterCell.ClientID;
				//s.RightId = cell.ClientID;
				//s.MinRight = Panels[i].MinValue;
				minRight = panel.MinValue;
				//list.Add(s);
				previewCell = cell;
				spliterCell.Attributes["onmousedown"] = String.Format("return Library.WebControl.Spliter.Register(event, this, {0}, {1}, {2})", minLeft, minRight, (Int32)Direction);
			}
		}

		protected void VerticalRender() {
			//var list = new List<Spliter>();
			//_renderSpliterList = list;
			TableRow row = null;
			TableCell previewCell = null;


			if (Panels.Count > 0) {
				row = new SpSpliterRow();
				_mainTable.Rows.Add(row);
				previewCell = Panels[0].CreateCell(String.Empty);//.CreateCell(ClientID + "pn0");
				row.Cells.Add(previewCell);
			}
			for (int i = 1; i < Panels.Count; i++) {
				var minLeft = 0;
				var minRight = 0;
				var panel = Panels[i];
				//var s = new Spliter();
				//s.LeftId = row.ClientID;
				minLeft = ((SpSpliterPanel)previewCell.Controls[0]).MinValue;
				minRight = panel.MinValue;
				row = new SpSpliterRow();
				//row.Height = new Unit(5);
				row.CssClass = "spSpliterV";
				row.Attributes["onmousedown"] = String.Format("return Library.WebControl.Spliter.Register(event, this, {0}, {1}, {2})", minLeft, minRight, (Int32)Direction);
				TableCell spliterCell = new SpSpliterTableCell();
				row.Cells.Add(spliterCell);
				_mainTable.Rows.Add(row);
				if (SpliterControlAdded != null) {
					SpliterControlAdded(this, new SpliterControlAddedEventArgs(row));
				}

				//spliterCell.CssClass = "spSpliterV";
				//row.ID = "pn" + (i - 1) + "_" + i;
				//s.MidleId = row.ClientID;

				//spliterCell.Width = new Unit(2);
				row.Cells.Add(spliterCell);
				row = new SpSpliterRow();
				TableCell cell = panel.CreateCell(String.Empty);
				row.Cells.Add(cell);
				_mainTable.Rows.Add(row);
				//s.RightId = row.ClientID;
				//s.MinRight = Panels[i].MinValue;
				//list.Add(s);
				previewCell = cell;
			}
		}

		//protected override void CreateClientObjectAtRender(HtmlTextWriter writer) {
		//  BeginScriptTag(writer);

		//  foreach (Spliter s in _renderSpliterList) {
		//    writer.Write("Library.WebControl.Spliter.Register('{0}', {1}, '{2}', '{3}', {4}, {5}, '{6}');", s.LeftId,
		//                 s.MinLeft, s.MidleId, s.RightId, s.MinRight, (Int32)Direction, _mainTable.ClientID);
		//  }
		//  EndScriptTag(writer);
		//}

		protected override void Render(HtmlTextWriter writer) {
			RenderSpliter();
			base.Render(writer);
		}

		public event SpliterControlAddedEventHandler SpliterControlAdded;

		//#region Nested type: Spliter

		//private class Spliter {
		//  //public string LeftId;
		//  //public string MidleId;
		//  public int MinLeft;
		//  public int MinRight;
		//  //public string RightId;
		//}

		//#endregion
	}
}